$(function() {
	var $dataTable = $('#dataTable');
	var $btnRefresh = $('#btnRefresh');
	
	initWidgets();
	initListener();
	initData();
	
	function initData() {
		var sid = $('#sid').val();
		var dtime = $('#dtime').val();
		$.ajax({
			url: 'details',
			type: 'GET',
			dataType: 'json',
			data: {
				sid: sid,
				dtime: dtime
			},
			//contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					var shelf = result.data;
					$('#shelfName').text(shelf.minerShelfName);
					$('#detectTime').text(shelf.detectTimeDisplay + ' (' + shelf.timeZoneDisplay + ')');
					$('#numAll').text(shelf.numAll);
					$('#numDetail').text(shelf.numDetail);
					$('#overallHashrate').text(formatMoney(shelf.overallReal, 2) + ' / ' + formatMoney(shelf.overallTheory, 2));
					$('#numPassed').text(shelf.numPassed);
					$('#ratePassed').text(formatMoney((shelf.ratePassed * 100), 2) + '%');
					$('#numUnpassed').text(shelf.numUnpassed);
					$('#numFailure').text(shelf.numFailure);
					
					$dataTable.bootstrapTable('load', shelf.details);
				}
			}
		});
	}
	
	function getTableHeight() {
	    return $(window).height() - 200;
	}
	
	function initListener() {
		$(window).resize(function () {
			$shelfTable.bootstrapTable('resetView', {
	            height: getTableHeight()
	        });
	    });
		
		$btnRefresh.on('click', function() {
			initData();
		});
	}
	
	function initWidgets() {
		$dataTable.bootstrapTable({
			height: getTableHeight(),
			toolbar: '#dataTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			rowStyle: function(row, index) {
				if (row.minerMac == null || row.minerMac == undefined || row.minerMac == '') {
					return {classes: 'active'};
				}
				var hashPass = row.hashPass;
				if (hashPass == 0) {
					hashPass = row.hashPassb;
				}
				if (row.hashReal >= hashPass) {
					return {classes: 'success'};
				} else {
					return {classes: 'danger'};
				}
				return {};
			},
			columns: [
				{
					field: 'minerHost',
					title: 'IP地址',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'minerMac',
					title: 'MAC地址',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'hashReal',
					title: '算力',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'fanSpeeds',
					title: '风扇',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'tempValues',
					title: '温度',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'chainAcns',
					title: '链条',
					halign: 'center',
					align: 'left',
					formatter: function(value, row, index) {
						if (!value) {
							return value;
						}
						var tip = '';
						if (row.chainAcss) {
							for (var i=0; i<row.chainAcss.length; i++) {
								tip += row.chainAcss[i] + '\n';
							}
						}
						return '<button class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="' + tip + '">' + value + '</button>'
					}
				}
			]
		});
	}
});